package com.hnkywl.sts.service.jsgl;

import com.hnkywl.sts.dao.cggl.GlfyDao;
import com.hnkywl.sts.dao.jsgl.*;
import com.hnkywl.sts.dao.organ.EjzhDao;
import com.hnkywl.sts.dao.sfgl.SkshDao;
import com.hnkywl.sts.dao.xsgl.DfkxsdDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.dao.xsgl.XsmxDao;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.cggl.*;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xsgl.Dfkxsd;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:58
 * To change this template use File | Settings | File Templates.
 */
@Service
public class FpService {
    private FpDao fpDao;
    private XsmxDao xsmxDao;
    private CgjsdmxDao cgjsdmxDao;
    private XsjsdmxDao xsjsdmxDao;
    private WljsdmxDao wljsdmxDao;
    private JsdfymxDao jsdfymxDao;
    private GlfyDao glfyDao;
    private WljsdService wljsdService;
    private EjzhDao ejzhDao;
    private BmService bmService;
    private SkshDao skshDao;
    private DfkxsdDao dfkxsdDao;


    @Transactional
    public String findAllfp(Integer type, Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize, Long ywgsID) {
        if ("".equals(gsJc)) {
            gsId = null;
        }
        Page<Jsd> fpPage = this.fpDao.findfpList(type, zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, pageNo, pageSize, ywgsID);
        List<Jsd> fpList = fpPage.getResult() == null ? new ArrayList<Jsd>() : fpPage.getResult();
        String fpJson = BaseUtils.fetchCollectionToJson(fpList, "id", "jsdh", "sqrq", "glqy.qyfr.nsh", "type", "glqy.id", "glqy.qc", "fpze", "ddfy", "fpzl", "hm", "sqr.id", "sqr.xm", "gs.id", "gs.jc", "cggs.id", "cggs.jc", "shzt", "kprq", "kpr.id", "kpr.xm","sftzd","zt","yjsd.id","swse");
        return "{\"total\":" + fpPage.getTotalCount() + ",\"rows\":[" + fpJson + "]}";
    }

    /**
     * 查询发票明细
     *
     * @param id
     * @return
     */
    @Transactional
    public Jsd findfpDetail(Long id) {
        return this.fpDao.get(id);
    }

    /**
     * 修改发票开票时间
     *
     * @param
     * @return
     */
    @Transactional
    public void xgkpsj(Jsd fp) {
        Jsd fp1 = this.findfpDetail(fp.getId());
        fp1.setKprq(fp.getKprq());
        this.fpDao.save(fp1);
    }


    /**
     * 审核状态
     *
     * @param id
     * @param status
     */
    @Transactional
    public void changeStatus(Long id, Integer status, String fphm, Long fpzs, String fpze, Jsd fp1) {
        Jsd fp = this.findfpDetail(id);
        if (fp.getType() == 1) {
            fp.setHm(fphm);
            fp.setFpzs(fpzs);
            fp.setFpze(Double.parseDouble(fpze.replaceAll(",", "")));
            if (fp1.getXsdmxList() != null) {
                for (Xsmx temp : fp1.getXsdmxList()) {
                    Xsmx clone = xsmxDao.findUniqueByProperty("id", temp.getId());
                    clone.setJsbhse(temp.getJsbhse());
                    clone.setJsse(temp.getJsse());
                    clone.setLastUpdated(new Date());
                    clone.setCreateUser(YgService.getCurrentYg());
                    clone.setCreateOrgan(YgService.getCurrentYg().getZggs());
                    clone.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                    xsmxDao.save(clone);
                }
            }
            //借方二级账户
            Ejzh jfzjzh = ejzhDao.findKyyeByKhId(fp.getXsdList().get(0).getSbgs().getId(),fp.getXsdList().get(0).getSbgs().getId());
            //贷方二级账户
            Ejzh dfzjzh = ejzhDao.findKyyeByKhId(fp.getXsdList().get(0).getKh().getId(),fp.getXsdList().get(0).getSbgs().getId());
            for(Xsmx xsmx :fp.getXsdmxList()){
               Double xse = 0.00;
            if(xsmx.getXsd() == null){
                xse += xsmx.getJsse();
                if(jfzjzh==null ){
                    throw  new RuntimeException("销售单"+xsmx.getXsd().getXsdh()+"申报公司二级账户为空");
                }
                if(dfzjzh==null  ){
                    throw  new RuntimeException("销售单"+xsmx.getXsd().getXsdh()+"客户二级账户为空");
                }
                //新增代付款销售单保存锁定数据
                Dfkxsd dfkxsd = new Dfkxsd();
                dfkxsd.setXsd(xsmx.getXsd());
                dfkxsd.setDateCreated(new Date());
                dfkxsd.setLastUpdated(dfkxsd.getDateCreated());
                dfkxsd.setDeleted(1);
                //新增收款
                Sfk sfk = new Sfk();
                sfk.setSbgs(xsmx.getXsd().getSbgs());
                sfk.setGldw(xsmx.getXsd().getKh());
                //外部付款
                sfk.setWlzlx(1);
                sfk.setDateCreated(new Date());
                sfk.setSfje(xse);
                sfk.setLastUpdated(sfk.getDateCreated());
                sfk.setCreateUser(null);
                sfk.setCreateOrgan(null);
                sfk.setCreateDepartment(null);
                sfk.setBm(this.bmService.getSfdbm("SK"));
                sfk.setSfklx(3);//销售收款
                sfk.setKxlx(4);//应收款
                Sjzdz pjlx = new Sjzdz();
                pjlx.setId(144L);
                sfk.setPjlx(pjlx);//票据类型  其他
                Sjzdz kxyt = new Sjzdz();
                kxyt.setId(132L);
                sfk.setKxyt(kxyt);
                sfk.setShrq(new Date());
                sfk.setZt("ysh");
                sfk.setSfkrq(new Date());
                dfkxsd.setMessage("销售收款");
                dfkxsd.setXssk(sfk);
                dfkxsdDao.save(dfkxsd);
                //贷方二级账户，买方二级账户
                sfk.setDfejzh(dfzjzh);
                //借方二级账户，卖方二级账户
                sfk.setJfejzh(jfzjzh);
                this.skshDao.save(sfk);
                }
                if(xse>0){
                if(dfzjzh.getKyye()<xse){
                    throw  new RuntimeException("二级账户"+dfzjzh.getQc()+"可用余额不足");
                }
                jfzjzh.setKyye((jfzjzh.getKyye()==null?0:jfzjzh.getKyye())+xse);
                jfzjzh.setYe((jfzjzh.getYe()==null?0:jfzjzh.getYe())+xse);
                dfzjzh.setKyye((dfzjzh.getKyye()==null?0:dfzjzh.getKyye())-xse);
                dfzjzh.setYe((dfzjzh.getYe()==null?0:dfzjzh.getYe())-xse);
                ejzhDao.save(jfzjzh);
                ejzhDao.save(dfzjzh);
                }
            }


        }

        if (fp.getType() == 2) {
            fp.setFpze(Double.parseDouble(fpze.replaceAll(",", "")));
            if (fp1.getWldList() != null) {
                for (Glfy temp : fp1.getWldList()) {
                    Glfy clone = glfyDao.findUniqueByProperty("id", temp.getId());
                    clone.setJsje(temp.getJsje());
                    clone.setSe(temp.getSe());
                    clone.setMe(temp.getMe());
                    clone.setCe(temp.getCe());
                    clone.setKdse(temp.getKdse());
                    clone.setLastUpdated(new Date());
                    clone.setCreateUser(YgService.getCurrentYg());
                    clone.setCreateOrgan(YgService.getCurrentYg().getZggs());
                    clone.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                    glfyDao.save(clone);
                }
            }
        }

        Yg kpr = YgService.getCurrentYg();
        if (fp1 != null) {
            fp.setDdfy(fp1.getDdfy());
            fp.setMe(fp1.getMe());
            fp.setSe(fp1.getSe());
            fp.setJe(fp1.getJe());
            fp.setCe(fp1.getCe());
        }
        fp.setKpr(kpr);
        fp.setKprq(new Date());
        fp.setShzt(1);
        fp.setZt(status + 1);
        this.fpDao.save(fp);
        if(fp.getType()==2){
            wljsdService.jtWljsd(fp);
        }
        this.saveJsdmx(fp);
    }

    /**
     * 退回发票                                                                                                 ;
     *
     * @param id
     * @param status
     */
    @Transactional
    public void changeStatusTh(Long id, Integer status, String thyy) {
        Jsd fp = this.findfpDetail(id);
        fp.setKpr(null);
        fp.setKprq(null);
        fp.setShzt(0);
        fp.setFpthyy(thyy.trim());
        fp.setZt(status);
        this.fpDao.save(fp);
    }

    @Transactional
    public void FpQxsh(Long id) {
        Jsd fp = this.findfpDetail(id);
        if (fp.getType() == 1) {
            fp.setHm("");
            fp.setFpzs(null);
        }
        fp.setShzt(0);
        fp.setZt(2);
        this.deleteJsdmx(fp);
        this.fpDao.save(fp);
    }

    @Transactional
    public void saveJsdmx(Jsd jsd) {
        if (jsd.getType() == 0) {
            List<Cgdmx> cgdmxList = jsd.getCgdmxList();
            for (Cgdmx cgdmx : cgdmxList) {
                Cgjsdmx cgjsdmx = new Cgjsdmx();
                cgjsdmx.setGg(cgdmx.getGg());
                cgjsdmx.setCd(cgdmx.getCd());
                cgjsdmx.setTh(cgdmx.getTh());
                cgjsdmx.setCk(cgdmx.getCk());
                cgjsdmx.setBhsj(cgdmx.getBhsj());
                cgjsdmx.setHsj(cgdmx.getHsj());
                cgjsdmx.setRksl(cgdmx.getRksl());
                cgjsdmx.setJe(cgdmx.getJsbhse());
                cgjsdmx.setSe(cgdmx.getJsse());
                cgjsdmx.setCe(cgdmx.getJsce());
                cgjsdmx.setJsd(jsd);
                cgjsdmx.setCgdmx(cgdmx);
                cgjsdmx.setCgd(cgdmx.getCgd());
                cgjsdmx.setCreateUser(YgService.getCurrentYg());
                cgjsdmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                cgjsdmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                cgjsdmx.setDateCreated(new Date());
                cgjsdmx.setLastUpdated(cgjsdmx.getDateCreated());
                this.cgjsdmxDao.save(cgjsdmx);
            }
        } else if (jsd.getType() == 1) {
            List<Xsmx> xsmxList = jsd.getXsdmxList();
            for (Xsmx xsmx : xsmxList) {
                Xsjsdmx xsjsdmx = new Xsjsdmx();
                xsjsdmx.setGg(xsmx.getJtwz());
                xsjsdmx.setKh(xsmx.getKh());
                xsjsdmx.setCk(xsmx.getCk());
                xsjsdmx.setBhsj(xsmx.getBhsj());
                xsjsdmx.setHsj(xsmx.getHsj());
                xsjsdmx.setCksl(xsmx.getCk_sbsl());
                xsjsdmx.setJe(xsmx.getJsbhse());
                xsjsdmx.setSe(xsmx.getJsse());
                xsjsdmx.setJsd(jsd);
                xsjsdmx.setXsmx(xsmx);
                xsjsdmx.setXsd(xsmx.getXsd());
                xsjsdmx.setCreateUser(YgService.getCurrentYg());
                xsjsdmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                xsjsdmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                xsjsdmx.setDateCreated(new Date());
                xsjsdmx.setLastUpdated(xsjsdmx.getDateCreated());
                this.xsjsdmxDao.save(xsjsdmx);
            }
        } else {
            List<Glfy> glfyList = jsd.getWldList();
            for (Glfy glfy : glfyList) {
                Wljsdmx wljsdmx = new Wljsdmx();
                wljsdmx.setSydw(glfy.getSydw());
                wljsdmx.setFylb(glfy.getFylb());
                wljsdmx.setDj(glfy.getDj());
                wljsdmx.setJe(glfy.getJe());
                wljsdmx.setJfsl(glfy.getJfsl());
                wljsdmx.setHddj(glfy.getHddj());
                wljsdmx.setHdje(glfy.getHdje());
                wljsdmx.setHdsl(glfy.getHdsl());
                wljsdmx.setKdse(glfy.getKdse());
                wljsdmx.setJsje(glfy.getJsje());
                wljsdmx.setSe(glfy.getSe());
                wljsdmx.setCe(glfy.getCe());
                wljsdmx.setMe(glfy.getMe());
                wljsdmx.setCgd(glfy.getCgd());
                wljsdmx.setXsd(glfy.getXsd());
                wljsdmx.setZkd(glfy.getZkd());
                wljsdmx.setType(glfy.getType());
                wljsdmx.setBz(glfy.getBz());
                wljsdmx.setGlfy(glfy);
                wljsdmx.setJsd(jsd);
                wljsdmx.setCreateUser(YgService.getCurrentYg());
                wljsdmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                wljsdmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                wljsdmx.setDateCreated(new Date());
                wljsdmx.setLastUpdated(wljsdmx.getDateCreated());
                this.wljsdmxDao.save(wljsdmx);

                List<Glfymx> glfymxList = glfy.getGlfymxList();
                for (Glfymx glfymx : glfymxList) {
                    Jsdfymx jsdfymx = new Jsdfymx();
                    jsdfymx.setGg(glfymx.getGg());
                    jsdfymx.setType(glfymx.getType());
                    jsdfymx.setJsjtfy(glfymx.getJsjtfy());
                    jsdfymx.setJtfy(glfymx.getJtfy());
                    jsdfymx.setRksl(glfymx.getRksl());
                    jsdfymx.setWljsdmx(wljsdmx);
                    jsdfymx.setCgdmx(glfymx.getCgdmx());
                    jsdfymx.setGlfymx(glfymx);
                    this.jsdfymxDao.save(jsdfymx);
                }
            }
        }
    }

    @Transactional
    public void deleteJsdmx(Jsd jsd){
        if (jsd.getType() == 0) {
            List<Cgjsdmx> cgjsdmxList = jsd.getCgjsdmxList();
            for(Cgjsdmx cgjsdmx:cgjsdmxList){
                cgjsdmx.setLastUpdated(new Date());
                cgjsdmx.setDeleted(1);
                this.cgjsdmxDao.save(cgjsdmx);
            }
        }else if(jsd.getType()==1){
            List<Xsjsdmx> xsjsdmxList = jsd.getXsjsdmxList();
            for(Xsjsdmx xsjsdmx:xsjsdmxList){
                xsjsdmx.setLastUpdated(new Date());
                xsjsdmx.setDeleted(1);
                this.xsjsdmxDao.save(xsjsdmx);
            }
        }else{
            List<Wljsdmx> wljsdmxList=jsd.getWljsdmxList();
            for(Wljsdmx wljsdmx:wljsdmxList){
                wljsdmx.setLastUpdated(new Date());
                wljsdmx.setDeleted(1);
                this.wljsdmxDao.save(wljsdmx);
                List<Jsdfymx> jsdfymxList=wljsdmx.getJsdfymxList();
                for(Jsdfymx jsdfymx:jsdfymxList){
                    jsdfymx.setLastUpdated(new Date());
                    jsdfymx.setDeleted(1);
                    this.jsdfymxDao.save(jsdfymx);
                }
            }
        }
    }


    //========================get and set==========================
    @Autowired
    public void setFpDao(FpDao fpDao) {
        this.fpDao = fpDao;
    }

    @Autowired
    public void setXsmxDao(XsmxDao xsmxDao) {
        this.xsmxDao = xsmxDao;
    }

    @Autowired
    public void setCgjsdmxDao(CgjsdmxDao cgjsdmxDao) {
        this.cgjsdmxDao = cgjsdmxDao;
    }

    @Autowired
    public void setXsjsdmxDao(XsjsdmxDao xsjsdmxDao) {
        this.xsjsdmxDao = xsjsdmxDao;
    }

    @Autowired
    public void setWljsdmxDao(WljsdmxDao wljsdmxDao) {
        this.wljsdmxDao = wljsdmxDao;
    }

    @Autowired
    public void setJsdfymxDao(JsdfymxDao jsdfymxDao) {
        this.jsdfymxDao = jsdfymxDao;
    }
    @Autowired
    public void setGlfyDao(GlfyDao glfyDao) {
        this.glfyDao = glfyDao;
    }
    @Autowired
    public void setWljsdService(WljsdService wljsdService) {
        this.wljsdService = wljsdService;
    }
    @Autowired
    public void setEjzhDao(EjzhDao ejzhDao) {
        this.ejzhDao = ejzhDao;
    }
    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }
    @Autowired
    public void setSkshDao(SkshDao skshDao) {
        this.skshDao = skshDao;
    }
    @Autowired
    public void setDfkxsdDao(DfkxsdDao dfkxsdDao) {
        this.dfkxsdDao = dfkxsdDao;
    }
}
